package com.vn.gofriends.fragment;

import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.parse.FindCallback;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.vn.gofriends.R;
import com.vn.gofriends.adapter.MenuAdapter;
import com.vn.gofriends.item.Trip;

public class ItemMyTripFragment extends Fragment {
	// Declare Variables
	Button btnAddGroup;
	MenuAdapter menuAdapter;
	GridView grMenu;
	TextView txtTitleItemTrip;

	Trip iTrip;
	ParseQuery<ParseObject> query;
	ParseObject userTraveler;
	ParseObject tripTraveler;
	String nameMember = "";
	LinearLayout linearUserName;
	boolean flagUserName = true;
	@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = null;
			view = inflater.inflate(R.layout.mytrips_layout, container, false);
			grMenu = (GridView) view.findViewById(R.id.grMenu);
			txtTitleItemTrip = (TextView) view.findViewById(R.id.txtTitleItemTrip);
			linearUserName = (LinearLayout) view.findViewById(R.id.linearUserName);
			//database
			String[] dataName = {"Map" , "Attactions" , "Tours&Activities","Hotel","Restaurants","Gas station"};
			int[] img = null;
			//end database
			Bundle bundle = this.getArguments();
			iTrip = (Trip) bundle.getSerializable("KeyTrip");
			txtTitleItemTrip.setText(iTrip.getTitle());
			//Check Member
			updateMember();
			//Check Member End
			//load data menu
			menuAdapter = new MenuAdapter(getActivity(), dataName, img);
			grMenu.setAdapter(menuAdapter);
			onSelectMenu();
			//end load data
			btnAddGroup = (Button) view.findViewById(R.id.btnAddGroup);
			btnAddGroup.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					final ParseObject t = new ParseObject("Traveler");
					ParseACL acl = new ParseACL();
					acl.setPublicReadAccess(true);
					t.setACL(acl);
					t.put("idUser", userTraveler);
					t.put("idTrip", tripTraveler);
					t.saveInBackground(new SaveCallback() {
						
						@Override
						public void done(ParseException e) {
							if(e==null){
								btnAddGroup.setEnabled(false);
								updateMember();
							}
						}
					});
				}
			});
			return view;
		}
	private void onSelectMenu() {
		
	}
	private void updateMember() {
		ParseQuery<ParseObject> queryTraveler = ParseQuery.getQuery("Traveler");
		queryTraveler.include("idUser");
		queryTraveler.include("idTrip");
		queryTraveler.findInBackground(new FindCallback<ParseObject>() {

			@Override
			public void done(List<ParseObject> travelers, ParseException e) {
				if (e == null) {
					for (ParseObject t : travelers) {
						String tripCurrent = t.getParseObject("idTrip").getObjectId();
						if (tripCurrent.equals(iTrip.getId())) {
							TextView txtUserName = new TextView(getActivity());
							txtUserName.setText(t.getParseObject("idUser").getString("username"));
							linearUserName.addView(txtUserName);
							if(txtUserName.getText().equals(ParseUser.getCurrentUser().getUsername())){
								flagUserName = false;
							}
								btnAddGroup.setEnabled(true);
								tripTraveler = t.getParseObject("idTrip");
								userTraveler = ParseUser.getCurrentUser();
						}
					}
					if(flagUserName){
						btnAddGroup.setEnabled(true);
						btnAddGroup.setTextColor(Color.BLACK);
					}
					else{
						btnAddGroup.setEnabled(false);
						btnAddGroup.setBackgroundColor(Color.TRANSPARENT);
					}
				} else {
					Log.d("Vien", e.getMessage());
				}
			}
		});
	}
}
